package com.jerry.huihome.module.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jerry.huihome.module.home.activity.MainActivity;

/**
 * Created by LGuipeng on 2016/5/11.
 */
public class GuidePageAdapter extends PagerAdapter{

    private final Context context;
    private final int[] inds;

    public GuidePageAdapter(Context context, int[] inds){
        this.context = context;
        this.inds = inds;
    }

    @Override
    public int getCount(){
        return inds.length;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position){
        ImageView imageview = new ImageView(context);
        imageview.setBackgroundResource(inds[position]);
        container.addView(imageview);
        if(position == (inds.length - 1)){
            imageview.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v){
                    Intent intent = new Intent();
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setClass(context, MainActivity.class);
                    context.startActivity(intent);
                    ((Activity) context).finish();
                    SharedPreferences sp = context.getSharedPreferences("drawHouse",Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putBoolean("isFirstRun",false);
                    edit.commit();
                }
            });
        }
        return imageview;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object){
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object){
        return view == object;
    }
}
